<template>
  <div>
    <!-- {{ text }}
    {{ oauthKey }} -->
    <div class="qrcode" ref="qrCodeUrl"></div>
    <div><button @click="showQrCode">点击扫码登录哔哩哔哩</button></div>
    <div><button @click="getLoginInfo">扫完码点击我</button></div>
    <div><button @click="getFollow">一键取消关注</button></div>
    <div><button @click="setCookie">手动设置cookie</button></div>
  </div>
</template>

<script>
import QRCode from 'qrcodejs2'
import qs from 'qs'
import Cookies from 'js-cookie'

export default {
  name: 'BilicancelApp',

  data() {
    return {
      vmid: 31100478,
      text: '',
      oauthKey: '',
      // fid: '',
      extend_content: '',
      csrf: '394f2a1dda8c74ce0d5efe4185054d5e'
    }
  },

  created() {},
  mounted() {},

  methods: {
    async showQrCode() {
      await this.getLoginUrl()
      await this.getLoginInfo()
      await this.creatQrCode()
    },
    creatQrCode() {
      this.qr = new QRCode(this.$refs.qrCodeUrl, {
        text: this.text,
        width: 200,
        height: 200,
        colorDark: '#000000',
        colorLight: '#ffffff',
        correctLevel: QRCode.CorrectLevel.H
      })
    },
    async getLoginUrl() {
      const { data: res } = await this.axios.get('/passport/qrcode/getLoginUrl')
      console.log(res)
      this.text = res.data.url
      this.oauthKey = res.data.oauthKey
    },
    async getLoginInfo() {
      const { data: res } = await this.axios.post('/passport/qrcode/getLoginInfo', qs.stringify({ oauthKey: this.oauthKey }))
      console.log(res)
      this.status = res.status
      if (this.status == true) {
        var url = res.data.url
        var item
        var index = url.indexOf('?')
        const cookie = {}
        var arr = url.slice(index + 1).split('&')
        for (item of arr) {
          const keyValue = item.split('=')
          cookie[keyValue[0]] = keyValue[1]
          Cookies.set(keyValue[0], keyValue[1])
        }
        // Cookies.set('cookie', JSON.stringify(cookie))
        console.log(cookie)
        this.vmid = cookie.DedeUserID
        this.csrf = cookie.bili_jct
        // localStorage.setItem('cookie', JSON.stringify(cookie))
      }
    },
    setCookie() {
      var item
      const cookie = {}
      var url =
        "_uuid=7F215A24-AF6E-3509-0FDC-B3A7080F1B0D46509infoc;buvid3=71B8DD40-4B3D-4988-A442-FE50FB9DF1AC167621infoc;DedeUserID=31100478;DedeUserID__ckMd5=c07ae4a995a93acc;rpdid=|(k|k)k~mkYk0J'uYJYlY||kk;video_page_version=v_old_home;LIVE_BUVID=AUTO6016392354797313;i-wanna-go-back=-1;b_ut=5;buvid4=A49465A0-4DEC-79BD-BF54-4A2D0DC3B0B329620-022012119-cWZGbE4cldphPQ6RQQ18wQ==;buvid_fp_plain=undefined;CURRENT_BLACKGAP=0;blackside_state=0;dy_spec_agreed=1;nostalgia_conf=-1;CURRENT_QUALITY=120;fingerprint3=25c08752cc962815c939b6d5707ef142;bsource=search_baidu;fingerprint=c596e11b29d78fb964d90f7cc6053641;SESSDATA=88101fad,1665121842,4decc*41;bili_jct=0dba509f5b43e74b0bbb2b7ef404d5ac;sid=jtn0954h;buvid_fp=41c6b0ce155e6df3738e80a4c0fb3924;innersign=1;CURRENT_FNVAL=4048;b_lsid=99547F10D_180169D6F8E;bp_video_offset_31100478=647720642152497200;bp_t_offset_31100478=647721492549730336;PVID=5"
      var arr = url.split(';')
      console.log(arr)
      for (item of arr) {
        const keyValue = item.split('=')
        console.log(keyValue)
        cookie[keyValue[0]] = keyValue[1]
        Cookies.set(keyValue[0], keyValue[1])
        this.vmid = cookie.DedeUserID
        this.csrf = cookie.bili_jct
      }
    },
    // async getFollow() {
    //   for (let j = 1; j <= 100; j++) {
    //     const { data: res } = await this.axios.get(`/api/x/relation/followings?vmid=31100478&pn=${j}&ps=20&order=desc&order_type=&jsonp=jsonp`)
    //     console.log(res)
    //     for (let i = 0; i < 20; i++) {
    //       // 提取关注列表id信息
    //       var fid1 = res.data.list[i].mid
    //       console.log(fid1)
    //       var data1 = {
    //         fid: '',
    //         act: '2',
    //         re_src: '11',
    //         spmid: '333.999.0.0',
    //         extend_content: '',
    //         jsonp: 'jsonp',
    //         csrf: this.csrf
    //       }
    //       data1['fid'] = fid1
    //       data1['extend_content'] = '{"entity":"user","entity_id":' + fid1 + '}'
    //       console.log(data1)
    //       // 请求查询id信息,判断最新发稿时间
    //       const { data: res1 } = await this.axios.get(`/api/x/space/arc/search?mid=${fid1}&pn=1&ps=1&index=1&jsonp=jsonp`)
    //       console.log(res1)
    //       if (res1.code == 0) {
    //         var ctime = res1.data.list.vlist[0].created
    //         console.log(ctime)
    //         if (ctime < 1609430400) {
    //           console.log('2021年后没再发稿,发起取消关注请求')
    //           const { data: res2 } = await this.axios.post('api/x/relation/modify', qs.stringify(data1))
    //           console.log(res2)
    //         }
    //       } else if (res1.code == -404) {
    //         console.log('啥视频也木有,直接发起取消关注请求')
    //         const { data: res3 } = await this.axios.post('api/x/relation/modify', qs.stringify(data1))
    //         console.log(res3)
    //       } else {
    //         console.log('该用户正常请继续关注')
    //       }
    //     }

    //   }
    // }
    getFollow() {
      var vmid = this.vmid
      var j = 1
      var i = 0
      var timer = setInterval(() => {
        console.log('这是 第' + j + '页')
        console.log('第' + i + '个up主')
        this.axios
          .get(`/api/x/relation/followings?vmid=${vmid}&pn=${j}&ps=20&order=desc&order_type=&jsonp=jsonp`)
          .then(res => {
            console.log(res)
            // 提取关注列表id信息
            var fid1 = res.data.data.list[i].mid
            // console.log(fid1)
            console.log(res.data.data.list[i].uname)
            console.log(res.data.data.list[i].sign)
            var data1 = {
              fid: '',
              act: '2',
              re_src: '11',
              spmid: '333.999.0.0',
              extend_content: '',
              jsonp: 'jsonp',
              csrf: this.csrf
            }
            data1['fid'] = fid1
            data1['extend_content'] = '{"entity":"user","entity_id":' + fid1 + '}'
            // console.log(data1)
            // 请求查询id信息,判断最新发稿时间
            this.axios
              .get(`/api/x/space/arc/search?mid=${fid1}&pn=1&ps=1&index=1&jsonp=jsonp`)
              .then(res1 => {
                // console.log(data1);
                // console.log(res1)
                if (res1.data.code == 0) {
                  if (res1.data.data.list.vlist.length == 0) {
                    console.log('该用户竟然没有视频!取消关注!')
                    this.axios
                      .post('api/x/relation/modify', qs.stringify(data1))
                      .then(res3 => {
                        console.log('取消关注成功!')
                      })
                      .catch(err => {
                        console.log(err)
                      })
                  } else {
                    var ctime = res1.data.data.list.vlist[0].created
                    // console.log(ctime)
                    if (ctime < 1609430400) {
                      console.log('2021年后没再发稿,发起取消关注请求')
                      this.axios
                        .post('api/x/relation/modify', qs.stringify(data1))
                        .then(res2 => {
                          console.log('取消关注成功!')
                        })
                        .catch(err => {
                          console.log(err)
                        })
                    } else {
                      console.log('该用户正常请继续关注')
                    }
                  }
                } else {
                  console.log('啥视频也木有,直接发起取消关注请求')
                  this.axios
                    .post('api/x/relation/modify', qs.stringify(data1))
                    .then(res3 => {
                      console.log('取消关注成功!')
                    })
                    .catch(err => {
                      console.log(err)
                    })
                }
              })
              .catch(err => {
                console.log(err)
              })
          })
          .catch(err => {
            console.log(err)
          })
        i++
        if (i == 20) {
          j++
          i = 0
        }
        if (j > 100) {
          clearInterval(timer)
        }
      }, 3000)
    }
  }
}
</script>

<style lang="less" scoped>
button {
  padding: 5px 10px;
  margin: 10px;
  background-color: pink;
  border-radius: 20px;
  border: none;
}
</style>
